/**
 * 缺失的第一个整数
 * LeetCode 41
 */


public class test7 {

    public int firstMissingPositive(int[] nums) {
        int l=0;
        int r = nums.length;
        while(l<r){
            if(nums[l]==l+1){
                l++;
            }else if(nums[l]<=l || nums[l]>r || nums[nums[l]-1]==nums[l]){
                swap(nums,l,--r);
            } else {
                swap(nums,l,nums[l]-1);
            }
        }
        return l+1;
    }

    public static void swap(int[] nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
